<?php
class my extends base{

	public function init(){
		parent::init();

		$this->current = 'my';
		$this->tit = '我的';

		$this->cateObj = D('company_cate');
		$this->modelObj = D('company_admin');

		$this->orderObj = D('company_order');

		$this->fankuiObj = D('company_fankui');
		$this->diaocha_logObj = D('company_diaocha_log');

	}

	public function index(){

		$cond = "status=1 and parent_id=0";
		$this->cates = $this->cateObj->findAll(array($cond));

		$cond = "status=1 and user_id={$this->user_id}".$this->cond_base;

		//权限
		// switch($this->admin['level']){
		// 	case '0'://管理员
		// 		$cond .=" ";
		// 		break;
		// 	case '1'://报修员
		// 		$cond .=" and admin_id={$this->admin_id}";
		// 		break;
		// 	case '2'://报修组长
		// 		$cond .=" ".$this->cond_cate.$this->cond_zone;
		// 		break;
		// 	case '3'://维修师傅
		// 		$cond .=" and user_id={$this->admin_id}";
		// 		break;
		// 	case '4'://维修组长
		// 		$cond .=" ".$this->cond_cate.$this->cond_zone;
		// 		break;
		// }

		$this->cond = $cond;

		$cond = $this->cond . " and order_status>2";
		$stat['order1'] = $this->fankuiObj->findCount(array($cond));

		$cond = $this->cond . " and order_status=2";
		$stat['order2'] = $this->fankuiObj->findCount(array($cond));

		$cond = $this->cond . " and order_status=3";
		$stat['order3'] = $this->fankuiObj->findCount(array($cond));

		$cond = $this->cond . " and order_status=4";
		$stat['order4'] = $this->fankuiObj->findCount(array($cond));

		$cond = $this->cond . " and order_status=5";
		$stat['order5'] = $this->fankuiObj->findCount(array($cond));



		$this->stat = $stat;


		// 上次提交的病床
		$last_bingchuang_id = 0;
		$cond = "status=1 and user_id={$this->user_id}".$this->cond_base;
		$log = $this->diaocha_logObj->find(array($cond));
		if($log){
			$last_bingchuang_id = $log['bingchuang_id'];
		}
		$this->last_bingchuang_id = $last_bingchuang_id;
	}

	public function setting(){

		if($this->fmdo == 'setting'){
			$arrBase = array(
				'face' =>arg('face'),
			);
			$this->modelObj->edit($this->admin_id, $arrBase);
			$this->renderSuccess('设置成功');
		}

		if($this->fmdo == 'sex'){
			$arrBase = array(
				'sex' =>arg('sex'),
			);
			$this->modelObj->edit($this->admin_id, $arrBase);
			$this->renderSuccess('设置成功');
		}

		if($this->fmdo == 'sign'){
			$arrBase = array(
				'is_sign' =>arg('sign'),
			);
			$this->modelObj->edit($this->admin_id, $arrBase);
			$this->renderSuccess('设置成功');
		}

		if($this->fmdo == 'zhiban'){
			$arrBase = array(
				'is_zhiban' =>arg('zhiban'),
			);
			$this->modelObj->edit($this->admin_id, $arrBase);
			$this->renderSuccess('设置成功');
		}
	}

	public function pwd(){

		if($this->fmdo == 'pwd'){
			$arrBase = array(
				'password' => qcing_hash(arg('pwd')),
			);
			$this->modelObj->edit($this->admin_id, $arrBase);
			$this->renderSuccess('修改成功');
		}
	}

	public function stat(){

		$cond = "status=1 ".$this->cond_base;


		//权限
		switch($this->admin['level']){
			case '0'://管理员
				$cond .=" ";
				break;
			case '1'://报修员
				$cond .=" and admin_id={$this->admin_id}";
				break;
			case '2'://报修组长
				$cond .=" ".$this->cond_cate.$this->cond_zone;
				break;
			case '3'://维修师傅
				$cond .=" and shifu_id={$this->admin_id}";
				break;
			case '4'://维修组长
				$cond .=" ".$this->cond_cate.$this->cond_zone;
				break;
		}
		//总订单
		$cond_sql0 = $cond . "";

		//今日订单
		$day1 = date("Y-m-d");
		$cond_sql1 = $cond . " and (add_time>='{$day1}')";
		$stat['count1'] = $this->orderObj->findCount(array($cond_sql1));

		//昨日订单
		// $day1 = date("Y-m-d");
		// $day2 = date("Y-m-d",strtotime("-1 day"));
		// $cond_sql2 = $cond . " and (add_time>='{$day1}' and add_time<='{$day2}')";
		//本周订单
		// $day1 = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
		// $cond_sql2 = $cond . " and (add_time>='{$day1}')";

		//本月订单
		$day1 = date("Y-m-01");
		$cond_sql2 = $cond . " and (add_time>='{$day1}')";
		$stat['count2'] = $this->orderObj->findCount(array($cond_sql2));
		//上月订单
		// $day1 = date('Y-m-d', strtotime('-1 month', strtotime(date('Y-m', time()) . '-01 00:00:00')));
		// $day2 = date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00') - 86400);
		// $cond_sql4 = $cond . " and (add_time>='{$day1}' and add_time<='{$day2}')";

		// 总量
		$cond_sql3 = $cond . " and order_status !=99";
		$stat['count3'] = $this->orderObj->findCount(array($cond_sql3));

        // 已完成
		$cond_sql4 = $cond . " and order_status>=5 and order_status<99";
		$stat['count4'] = $this->orderObj->findCount(array($cond_sql4));

        // 未完成
		$cond_sql5 = $cond . " and order_status>=5 and order_status<99";
		$stat['count5'] = $this->orderObj->findCount(array($cond));

		$this->stat = $stat;

	}

	public function sign(){
		if($this->fmdo == 'sign'){
			$arrBase = array(
				'is_sign' =>arg('sign'),
			);
			$this->modelObj->edit($this->admin_id, $arrBase);
			$this->renderSuccess('设置成功');
		}
	}


	public function score_log(){

		$arrBase = array('tab' => $this->tab,'page' => '0000',);
		if($this->keyword){
			$arrBase = array_merge($arrBase,array('keyword' => $this->keyword));
		}
		$this->nextpage = url($this->routeUrl,$this->action.'_loaddata', $arrBase);

	}

	//loaddata
	public function score_log_loaddata(){

		$cond = "status=1 and user_id={$this->user_id}";
		//搜索
		if($this->keyword){
			$cond.=" and (msg like '%".$this->keyword."%' ) ";
		}

		$result = $this->score_logObj->get_loaddata($cond);
		if($result){
			$this->list = $result;
		}else{
			die('0');
		}
	}

}